package com.dtjd.playcoinmonkey.utils;

import android.os.StrictMode;
import com.dtjd.playcoinmonkey.BuildConfig;
import com.dtjd.playcoinmonkey.MyApplication;
import com.dtjd.playcoinmonkey.bean.HttpParaBean;

import java.io.File;
import java.util.List;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
public class Network {
    public static void post(List<HttpParaBean> para, String url, Callback callback){
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
        FormBody.Builder body = new FormBody.Builder();
        for (HttpParaBean bean : para) {
            if (bean.name.equals("token")){
                body.add("token", MyApplication.sp.getString("token",""));
            }else{
                body.add(bean.name,bean.value);
            }
        }
        body.add("language","1");
        new OkHttpClient().newCall(new Request.Builder().url(BuildConfig.BASE_URL+url).post(body.build()).build()).enqueue(callback);
    }
    public static void upload(String url, File file, List<HttpParaBean> para,Callback callback){
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
        String language = MyApplication.sp.getString("language", "");
        if (language.equals("Chinese")){
            language="1";
        }else {
            language="0";
        }
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", "", RequestBody.create(MediaType.parse("multipart/form-data"), file)).addFormDataPart("language",language);
        for (HttpParaBean bean : para) {
            if (bean.name.equals("token")){
                builder.addFormDataPart("token", MyApplication.sp.getString("token",""));
            }else {
                builder.addFormDataPart(bean.name,bean.value);
            }
        }
        new OkHttpClient().newCall(new Request.Builder().url(BuildConfig.BASE_URL+url).post(builder.build()).build()).enqueue(callback);
    }
}